using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACME.Northwind.DataTransfer;
using System.Linq.Expressions;
using ACME.Northwind.Service.Interfaces;

namespace ACME.Northwind.WCFProxy
{
	/// <summary>
	/// The service implementation for Product object
	/// </summary>
	public partial class ProductService : IProductService
	{
		#region IProductService Members

		/// <summary>
		/// Run a selection by the primary key
		/// </summary>
		public ProductDTO SelectByPrimaryKey(int productid)
		{
			return DataService.Instance.GetProduct(productid);
		}

		/// <summary>
		/// Run the persist for a list
		/// </summary>
		public bool Persist(List<ProductDTO> list)
		{
			bool retval = true;
			foreach (ProductDTO product in list)
			{
				retval &= this.Persist(product);
			}
			return retval;
		}

		/// <summary>
		/// Run the persist for an object
		/// </summary>
		public bool Persist(ProductDTO dto)
		{
			return (DataService.Instance.PersistProduct(dto) != null);
		}

		/// <summary>
		/// Run a selection using LINQ
		/// </summary>
		public List<ProductDTO> RunSelect(string linq)
		{
			List<ProductDTO> retVal = new List<ProductDTO>();
			retVal.AddRange(DataService.Instance.GetProductListByLINQ(linq));
			return retVal;
		}

		/// <summary>
		/// Run a paginated selection using LINQ
		/// </summary>
		public PagedQueryResults<ProductDTO> RunSelect(int pageIndex, int pageSize, bool ascending, string sortColumn, string linq)
		{
			return DataService.Instance.GetProductListPaged(pageIndex, pageSize, ascending, sortColumn, linq);
		}

		/// <summary>
		/// Run a selection for all objects
		/// </summary>
		public List<ProductDTO> RunSelect()
		{
			List<ProductDTO> retVal = new List<ProductDTO>();
			DataServiceClient ds = DataService.Instance;
			retVal.AddRange(ds.GetProductList());
			return retVal;
		}

		/// <summary>
		/// Return a list of related OrderDetail objects
		/// </summary>
		public List<OrderDetailDTO> GetOrderDetailList(ProductDTO item)
		{
			List<OrderDetailDTO> retval = new List<OrderDetailDTO>();
			DataServiceClient ds = DataService.Instance;
			string s = "";
			s += "x.ProductId = " + item.ProductId + "";
			retval.AddRange(ds.GetOrderDetailListByLINQ("x => " + s));
			return retval;
		}

		/// <summary>
		/// Return the related Category item
		/// </summary>
		public CategoryDTO GetCategoryItem(ProductDTO item)
		{
			CategoryDTO retval = new CategoryDTO();
			if (false ||
				(item.CategoryId == null) ||
				false
			)
			{
				//The primary key cannot be null, so there is no object
				return null;
			}
			DataServiceClient ds = DataService.Instance;
			retval = ds.GetCategory(item.CategoryId.Value);
			return retval;
		}

		/// <summary>
		/// Return the related Supplier item
		/// </summary>
		public SupplierDTO GetSupplierItem(ProductDTO item)
		{
			SupplierDTO retval = new SupplierDTO();
			if (false ||
				(item.SupplierId == null) ||
				false
			)
			{
				//The primary key cannot be null, so there is no object
				return null;
			}
			DataServiceClient ds = DataService.Instance;
			retval = ds.GetSupplier(item.SupplierId.Value);
			return retval;
		}

		/// <summary>
		/// Delete the specified item
		/// </summary>
		public bool Delete(ProductDTO item)
		{
			DataServiceClient ds = DataService.Instance;
			return ds.DeleteProduct(item);
		}

		#endregion
	}
}
